home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / dev / lang / sofa.lha / sofa / smalleiffel / sys / runtime / deep_twin.c < prev    next >
Text File  |  2000-03-25  |  5KB  |  157 lines

  1. /*
  2. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  3. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  4. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  5. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  6. -- this header is kept unaltered, and a notification of the changes is added.
  7. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  8. -- another product.
  9. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  10. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  11. --                       http://SmallEiffel.loria.fr
  12. --
  13. */
  14. /*
  15.   This file (SmallEiffel/sys/runtime/deep_twin.c) contains the basic
  16.   runtime support for the GENERAL.deep_twin feature as well as the basic
  17.   runtime support for the GENERAL.is_deep_equal feature.
  18.   This file is automatically included in the header when `deep_twin' or
  19.   `is_deep_equal' is in the live code of the system.
  20. */
  21.  
  22. /* To count level of nested `deep_twin' calls:
  23.  */
  24. static int se_deep_twin_start_counter = 0;
  25.  
  26. /* Memory buffer to retrieve already `deep_twin'ed objects:
  27.  */
  28. static void** se_deep_twin_memory1 = NULL;
  29. static void** se_deep_twin_memory2 = NULL;
  30. static size_t se_deep_twin_memory_sizeof = 0;
  31. static int    se_deep_twin_memory_last = -1;
  32.  
  33. #define SE_DEEP_TWIN_BUFFER_SIZE 128
  34.  
  35. void se_deep_twin_start(void) {
  36.   if (se_deep_twin_start_counter == 0) {
  37.     if (se_deep_twin_memory_sizeof == 0) {
  38.       int s;
  39.       se_deep_twin_memory_sizeof = SE_DEEP_TWIN_BUFFER_SIZE;
  40.       s = (se_deep_twin_memory_sizeof * sizeof(void*));
  41.       se_deep_twin_memory1 = ((void**)malloc(s));
  42.       se_deep_twin_memory2 = ((void**)malloc(s));
  43.     }
  44.   }
  45.   se_deep_twin_start_counter++;
  46. }
  47.  
  48. void* se_deep_twin_search(void* object) {
  49.   int i = se_deep_twin_memory_last;
  50.   for (i = se_deep_twin_memory_last; i >= 0; i--) {
  51.     if (object == se_deep_twin_memory1[i]) {
  52.       return (se_deep_twin_memory2[i]);
  53.     }
  54.   }
  55.   return NULL;
  56. }
  57.  
  58. void se_deep_twin_register(void* object, void* deep_twin) {
  59.   if (object != NULL) {
  60.     if (se_deep_twin_search(object) == NULL) {
  61.       if (se_deep_twin_memory_last == se_deep_twin_memory_sizeof) {
  62.     int s;
  63.     se_deep_twin_memory_sizeof *= 2;
  64.     s = (se_deep_twin_memory_sizeof * sizeof(void*));
  65.     se_deep_twin_memory1 = ((void**)realloc(se_deep_twin_memory1,s));
  66.     se_deep_twin_memory2 = ((void**)realloc(se_deep_twin_memory2,s));
  67.       }
  68.       se_deep_twin_memory_last++;
  69.       se_deep_twin_memory1[se_deep_twin_memory_last] = object;
  70.       se_deep_twin_memory2[se_deep_twin_memory_last] = deep_twin;
  71.     }
  72.   }
  73. }
  74.  
  75. void* se_deep_twin_trats(void* object) {
  76.   se_deep_twin_start_counter--;
  77.   if (se_deep_twin_start_counter == 0) {
  78.     se_deep_twin_memory_last = -1;
  79.     if (se_deep_twin_memory_sizeof > SE_DEEP_TWIN_BUFFER_SIZE) {
  80.       free(se_deep_twin_memory1);
  81.       free(se_deep_twin_memory2);
  82.       se_deep_twin_memory1 = NULL;
  83.       se_deep_twin_memory2 = NULL;
  84.       se_deep_twin_memory_sizeof = 0;
  85.     }
  86.   }
  87.   return object;
  88. }
  89.  
  90.  
  91. /* To count level of nested `deep_equal' calls:
  92.  */
  93. static int se_deep_equal_start_counter = 0;
  94.  
  95. /* Memory buffer to retrieve already `deep_equal'ed objects:
  96.  */
  97. static void** se_deep_equal_memory1 = NULL;
  98. static void** se_deep_equal_memory2 = NULL;
  99. static size_t se_deep_equal_memory_sizeof = 0;
  100. static int    se_deep_equal_memory_last = -1;
  101.  
  102. void se_deep_equal_start(void) {
  103.   if (se_deep_equal_start_counter == 0) {
  104.     if (se_deep_equal_memory_sizeof == 0) {
  105.       int s;
  106.       se_deep_equal_memory_sizeof = SE_DEEP_TWIN_BUFFER_SIZE;
  107.       s = (se_deep_equal_memory_sizeof * sizeof(void*));
  108.       se_deep_equal_memory1 = ((void**)malloc(s));
  109.       se_deep_equal_memory2 = ((void**)malloc(s));
  110.     }
  111.   }
  112.   se_deep_equal_start_counter++;
  113. }
  114.  
  115. static void se_deep_equal_register(void* object, void* deep) {
  116.   if (se_deep_equal_memory_last == se_deep_equal_memory_sizeof) {
  117.     int s;
  118.     se_deep_equal_memory_sizeof *= 2;
  119.     s = (se_deep_equal_memory_sizeof * sizeof(void*));
  120.     se_deep_equal_memory1 = ((void**)realloc(se_deep_equal_memory1,s));
  121.     se_deep_equal_memory2 = ((void**)realloc(se_deep_equal_memory2,s));
  122.   }
  123.   se_deep_equal_memory_last++;
  124.   se_deep_equal_memory1[se_deep_equal_memory_last] = object;
  125.   se_deep_equal_memory2[se_deep_equal_memory_last] = deep;
  126. }
  127.  
  128. int se_deep_equal_search(void* object,void* deep) {
  129.   int i;
  130.   if (object == deep) return 1;
  131.   if ((object != NULL) && (deep != NULL)) {
  132.     for (i = se_deep_equal_memory_last; i >= 0; i--) {
  133.       if (object == se_deep_equal_memory1[i]) {
  134.     if (deep == se_deep_equal_memory2[i]) {
  135.       return 1;
  136.     }
  137.       }
  138.     }
  139.     se_deep_equal_register(object,deep);
  140.   }
  141.   return 0;
  142. }
  143.  
  144. void se_deep_equal_trats(void) {
  145.   se_deep_equal_start_counter--;
  146.   if (se_deep_equal_start_counter == 0) {
  147.     se_deep_equal_memory_last = -1;
  148.     if (se_deep_equal_memory_sizeof > SE_DEEP_TWIN_BUFFER_SIZE) {
  149.       free(se_deep_equal_memory1);
  150.       free(se_deep_equal_memory2);
  151.       se_deep_equal_memory1 = NULL;
  152.       se_deep_equal_memory2 = NULL;
  153.       se_deep_equal_memory_sizeof = 0;
  154.     }
  155.   }
  156. }
  157.